home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3006 / 3006.xpi / components / dhCopyUrlProcessor.js < prev    next >
Text File  |  2010-01-15  |  6KB  |  173 lines

  1. /******************************************************************************
  2.  *            Copyright (c) 2006-2009 Michel Gutierrez. All Rights Reserved.
  3.  ******************************************************************************/
  4.  
  5. /**
  6.  * Constants.
  7.  */
  8.  
  9. const NS_COPYURL_CID = Components.ID("{93e81622-ce06-410e-bc10-4f3dd7617399}");
  10. const NS_COPYURL_PROG_ID = "@downloadhelper.net/copyurl-processor;1";
  11. const DHNS = "http://downloadhelper.net/1.0#";
  12.  
  13. var Util=null;
  14.  
  15. /**
  16. * Object constructor
  17. */
  18. function CopyUrl() {
  19.     try {
  20.         //dump("[CopyUrl] constructor\n");
  21.         this.core=Components.classes["@downloadhelper.net/core;1"].
  22.             getService(Components.interfaces.dhICore);
  23.         this.core.registerProcessor(this);
  24.     } catch(e) {
  25.         dump("[CopyUrl] !!! constructor: "+e+"\n");
  26.     }
  27. }
  28.  
  29. CopyUrl.prototype = {
  30.         get name() { return "copyurl"; },
  31.         get provider() { return "DownloadHelper"; },
  32.         get title() { return Util.getText("processor.copyurl.title"); },
  33.         get description() { return Util.getText("processor.copyurl.description"); },
  34.         get enabled() { return true; },
  35. }
  36.  
  37. CopyUrl.prototype.canHandle=function(desc) {
  38.     //dump("[CopyUrl] canHandle()\n");
  39.     var ch=desc.has("media-url");
  40.     return ch;
  41. }
  42.  
  43. CopyUrl.prototype.requireDownload=function(desc) {
  44.     //dump("[CopyUrl] requireDownload()\n");
  45.     return false;
  46. }
  47.  
  48. CopyUrl.prototype.preDownload=function(desc) {
  49.     return true;
  50. }
  51.  
  52. CopyUrl.prototype.handle=function(desc) {
  53.     //dump("[CopyUrl] handle()\n");
  54.     var mediaUrl=Util.getPropsString(desc,"media-url");
  55.     if(mediaUrl) {
  56.         var str = Components.classes["@mozilla.org/supports-string;1"].
  57.             createInstance(Components.interfaces.nsISupportsString); 
  58.         if (!str) return; 
  59.         str.data = mediaUrl; 
  60.         var trans = Components.classes["@mozilla.org/widget/transferable;1"].
  61.             createInstance(Components.interfaces.nsITransferable);
  62.         if (!trans) return; 
  63.         trans.addDataFlavor("text/unicode"); 
  64.         trans.setTransferData("text/unicode",str,mediaUrl.length * 2); 
  65.         var clipid = Components.interfaces.nsIClipboard; 
  66.         var clip = Components.classes["@mozilla.org/widget/clipboard;1"].
  67.             getService(clipid); 
  68.         if (!clip) return; 
  69.         clip.setData(trans,null,clipid.kGlobalClipboard);
  70.         //dump("[CopyUrl] handle(): to clipboard "+mediaUrl+"\n");
  71.     }
  72. }
  73.  
  74. CopyUrl.prototype.QueryInterface = function(iid) {
  75.     //dump("[CopyUrl] QueryInterface("+iid+")\n");
  76.     if(
  77.            iid.equals(Components.interfaces.dhIProcessor) ||
  78.         iid.equals(Components.interfaces.nsISupports)
  79.     ) {
  80.         return this;
  81.     }
  82.     throw Components.results.NS_ERROR_NO_INTERFACE;
  83. }
  84.  
  85. var vCopyUrlModule = {
  86.     firstTime: true,
  87.     
  88.     /*
  89.      * RegisterSelf is called at registration time (component installation
  90.      * or the only-until-release startup autoregistration) and is responsible
  91.      * for notifying the component manager of all components implemented in
  92.      * this module.  The fileSpec, location and type parameters are mostly
  93.      * opaque, and should be passed on to the registerComponent call
  94.      * unmolested.
  95.      */
  96.     registerSelf: function (compMgr, fileSpec, location, type) {
  97.  
  98.         if (this.firstTime) {
  99.             this.firstTime = false;
  100.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  101.         }
  102.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  103.         compMgr.registerFactoryLocation(NS_COPYURL_CID,
  104.                                         "CopyUrl",
  105.                                         NS_COPYURL_PROG_ID, 
  106.                                         fileSpec,
  107.                                         location,
  108.                                         type);
  109.     },
  110.  
  111.     unregisterSelf: function(compMgr, fileSpec, location) {
  112.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  113.         compMgr.unregisterFactoryLocation(NS_DH_COPYURL_CID, fileSpec);
  114.     },
  115.  
  116.     /*
  117.      * The GetClassObject method is responsible for producing Factory and
  118.      * SingletonFactory objects (the latter are specialized for services).
  119.      */
  120.     getClassObject: function (compMgr, cid, iid) {
  121.         if (!cid.equals(NS_COPYURL_CID)) {
  122.             throw Components.results.NS_ERROR_NO_INTERFACE;
  123.         }
  124.  
  125.         if (!iid.equals(Components.interfaces.nsIFactory)) {
  126.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  127.         }
  128.  
  129.         return this.vCopyUrlFactory;
  130.     },
  131.  
  132.     /* factory object */
  133.     vCopyUrlFactory: {
  134.         /*
  135.          * Construct an instance of the interface specified by iid, possibly
  136.          * aggregating it with the provided outer.  (If you don't know what
  137.          * aggregation is all about, you don't need to.  It reduces even the
  138.          * mightiest of XPCOM warriors to snivelling cowards.)
  139.          */
  140.         createInstance: function (outer, iid) {
  141.             if (outer != null) {
  142.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  143.             }
  144.     
  145.             if(Util==null) 
  146.                 Util=Components.classes["@downloadhelper.net/util-service;1"]
  147.                     .getService(Components.interfaces.dhIUtilService);
  148.  
  149.             return new CopyUrl().QueryInterface(iid);
  150.         }
  151.     },
  152.  
  153.     /*
  154.      * The canUnload method signals that the component is about to be unloaded.
  155.      * C++ components can return false to indicate that they don't wish to be
  156.      * unloaded, but the return value from JS components' canUnload is ignored:
  157.      * mark-and-sweep will keep everything around until it's no longer in use,
  158.      * making unconditional ``unload'' safe.
  159.      *
  160.      * You still need to provide a (likely useless) canUnload method, though:
  161.      * it's part of the nsIModule interface contract, and the JS loader _will_
  162.      * call it.
  163.      */
  164.     canUnload: function(compMgr) {
  165.         return true;
  166.     }
  167. };
  168.  
  169. function NSGetModule(compMgr, fileSpec) {
  170.     return vCopyUrlModule;
  171. }
  172.  
  173.